;;;functional programs encode mathematical truths
(define (fact n)
  (cond ((= n 1) 1)
        (else (* n (fact (- n 1))))))
 
;;;processes evolved by such programs
;;;can be understood by substitution
 
;;;methods may be distinguished by the choice of truths expressed
;;;递归过程
(define (+ n m)
  (cond ((= n 0) m)
        (else (1+ (+ (-1+ n) m)))))
 
;;;迭代过程
(define (+ n m)
  (cond ((= n 0) m)
        (else (+ (-1+ n) (1+ m)))))
 
 
;;; <before>
;;;(set! <var> <value>)
;;; <after>
 
;;;    |
;;;    |
;;;------------------
;;;    |
;;;    | <var> has the
;;;    | value <value>
;;;    |
;;;    |
;;;    |
;;;    V
;;;    时间线
;;;
 
 
(define count 1)
(define (demo x)
  (set! count (1+ count))
  (+ x count))
;;;(demo 3) ==> 5
;;;(demo 3) ===> 6
 
 
;;;其中的i为时间
;;;函数式版本
(define (fact n)
  (define (iter m i)
    (cond ((> i n) m)
          (else (iter (* i m) (+ i 1)))))
  (iter 1 1))
 
;;;imperative version
;;;命令式版本
(define (fact n)
  (let ((i 1) (m 1))
    (define (loop)
      (cond ((> i n) m)
            (else
             (set! m (* i m))
             (set! i (+ i 1))
             (loop))))
    (loop)))
 
;;;(let ((var1 e1) (var2 e2))
;;;     e3)
 
;;;we say that a variable, v, is "bound in a expression". E.
;;;if the meaning of E is unchanged by the uniform replacement of
;;;a variable. W. not occurring in E, for every occurrence of V in E.
 
 
;;;bound variables
(lambda (y)
  ((lambda (x)
    (* x y)) 3))
;;;等价于
(lambda (y)
  ((lambda (z)
    (* z y)) 3))
 
 
;;;自由变量
;;;we say that a variable, V, is "free in an expression". E. If the
;;;meaning of E is changed by the uniform replacement of
;;;a variable. W. not occurring in E. for every occurrence of V in E.
 
(lambda (y)
  ((lambda (x)
     (* x y)) 3)) ;;在这个式子（表达式）中，*为自由变量，因为如果用+替换，整个表达式的含义就变了
 
;;;作用域scope
;;;If x is a bound variable in e,
;;;then there is a lambda expression where it is bound.
;;;We call the list of formal parameters of the lambda expression
;;;the "bound variable list" and we say that the lambda expression "binds"
;;;the variables "declared" in its bound variable list. In addition, those
;;;parts of the expression where a variable has a value defined by the
;;;lambda expression which binds it is called the "scope" of the variable.
 
 
;;;环境是执行虚拟代换的一种方式
;;;an environment is a way of doing substitutions virtually
;;;是存储你的未完成的代换的地方
;;;it's a place where something is stored
;;;which is the substitutions that you haven't done
 
 
;;;rule 1: a procdedure object is applied to a set of arguments by
;;;constructing a frame. binding the formal parameters of the procedure
;;;to the actual arguments of the call. and then evaluating the body of the
;;;procedure in the context of the new environment constructed. The new frame
;;;has as its enclosing environment. the environment part of the procedure
;;;object being applied.
 
 
;;;rule 2: a lambda-expression is evaluated relative to a given environment
;;;as follows: a new procedure object is formed. combining the text (codec) of the
;;;lambda expression with a pointer to the environment of evaluation.
 
;;;过程体+环境*********重点**********
;;;有个变量指向过程体
 
;;;第一步，在全局环境中求值过程体表达式(make-counter 0)
;;;第二步，查找全局环境找到make-couonter
;;;第三步，应用这个过程
;;;第四步，创建一个新的框架，里面为n=0
;;;第五步，创建一个新的过程对象，代码为(lambda () ... )
;;;
 
 
;;;什么是对象？
 
 
;;;actions and indentity
;;;we say that an action. A. had an effect on an object. X. (for
;;;equivalently. that X was changed by A) if some property. P. which
;;;was true of X before A became false of X after A.
 
;;;We say that two objects. X and Y. are the same if any action which
;;;has an effect on X has the same effect on Y.
 
 
;;;Cesaro's method for estimating pi: prob(gcd(n1, n2) = 1) = 6/(pi*pi)
(define (estinamte-pi n)
  (sqrt (/ 6 (monte-cario n cesaro))))
(define (cesaro)
  (= (gcd (rand) (rand)) 1))
(define (monte-carlo trials experiment)
  (define (iter remaining passed)
    (cond ((= remaining 0)
           (/ passed trials))
          ((experiment)
           (iter (-1+ remaining) (1+ passed)))
          (else (iter (-1+ remaining) passed))))
  (iter trials 0))
(define rand
  (let ((x random-init))
    (lambda ()
      (set! x (rand-update x))
      x)))
 
